#coding=utf-8
from django.conf import settings

from django.contrib import admin
from django.contrib.sites.models import Site

__author__ = 'czq'

from django.contrib.admin.views.main import ChangeList
class MyChangeList(ChangeList):
    def get_results(self, request):
        if request.user.is_superuser==True:
            pass
        else:
            self.query_set=self.query_set.filter(addUser=request.user)
        super(MyChangeList,self).get_results(request)

#绑定用户
class UserModelAdmin(admin.ModelAdmin):
    def get_changelist(self, request, **kwargs):
        return MyChangeList

    def save_model(self, request, obj, form, change):
        if (not change):
            obj.addUser=request.user
            #obj.site=Site.objects.get(id=settings.SITE_ID)
        super(UserModelAdmin,self).save_model(request,obj,form,change)
    def delete_model(self, request, obj):
        if request.user==obj.addUser:
            super(UserModelAdmin,self).delete_model(request,obj)
        else:
            pass

#绑定网站和用户
class SiteUserModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        if (not change):
            obj.addUser=request.user
            obj.site=Site.objects.get(id=settings.SITE_ID)
        super(SiteUserModelAdmin,self).save_model(request,obj,form,change)
    exclude = ('site','addUser')
#绑定网站
class SiteModelAdmin(admin.ModelAdmin):
    exclude = ('site',)

    def save_model(self, request, obj, form, change):
        if (not change):
            #obj.addUser=request.user

            obj.site=Site.objects.get(id=settings.SITE_ID)
        super(SiteModelAdmin,self).save_model(request,obj,form,change)



class MyModelAdmin(admin.ModelAdmin):


    def save_model(self, request, obj, form, change):
        if (not change):
            obj.addUser=request.user
        super(MyModelAdmin,self).save_model(request,obj,form,change)
    def delete_model(self, request, obj):
        if request.user==obj.addUser:
            super(MyModelAdmin,self).delete_model(request,obj)
        else:
            pass
